<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类" prop="classifyId">
        <el-select v-model="queryParams.classifyId" placeholder="请选择分类" clearable>
          <el-option
            v-for="item in classifyList"
            :key="item.id"
            :label="item.classifyName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="作者" prop="authorId">
        <el-select v-model="queryParams.authorId" placeholder="请选择作者" clearable>
          <el-option
            v-for="item in authorList"
            :key="item.id"
            :label="item.authorStageName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐" prop="bookIsRecommend">
        <el-select v-model="queryParams.bookIsRecommend" placeholder="请选择是否推荐" clearable>
          <el-option
            v-for="dict in dict.type.sys_novel_recommend"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="书名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入书名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否付费" prop="bookIsPay">
        <el-select v-model="queryParams.bookIsPay" placeholder="请选择是否付费" clearable>
          <el-option
            v-for="dict in dict.type.sys_novel_money"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="书籍状态" prop="bookStatus">
        <el-select v-model="queryParams.bookStatus" placeholder="请选择书籍状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_novel_display"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['novel:book:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['novel:book:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['novel:book:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['novel:book:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="分类" align="center" prop="classifyName" />
      <el-table-column label="作者" align="center" prop="authorName" />
      <el-table-column label="是否推荐" align="center" prop="bookIsRecommend">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_novel_recommend" :value="scope.row.bookIsRecommend"/>
        </template>
      </el-table-column>
      <el-table-column label="书名" align="center" prop="bookName" />
      <el-table-column label="书籍封面" align="center" prop="bookImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bookImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="书籍字数" align="center" prop="bookFontNumber" />
      <el-table-column label="更新说明" align="center" prop="bookUpdateText" />
      <el-table-column label="发布时间" align="center" prop="bookStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="bookUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookUpdateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完结时间" align="center" prop="bookEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="书籍评分" align="center" prop="bookScore" />
      <el-table-column label="浏览量" align="center" prop="bookBrowseNumber" />
      <el-table-column label="收藏数量" align="center" prop="bookCollectionNumber" />
      <el-table-column label="点赞数量" align="center" prop="bookLikeNumber" />
      <el-table-column label="是否付费" align="center" prop="bookIsPay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_novel_money" :value="scope.row.bookIsPay"/>
        </template>
      </el-table-column>
      <el-table-column label="付费金额" align="center" prop="bookPayMoney" />
      <el-table-column label="付费数量" align="center" prop="bookPayNumber" />
      <el-table-column label="书籍状态" align="center" prop="bookStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_novel_display" :value="scope.row.bookStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['novel:book:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['novel:book:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改书籍列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类" prop="classifyId">
          <el-select v-model="form.classifyId" placeholder="请选择分类">
            <el-option
              v-for="item in classifyList"
              :key="item.id"
              :label="item.classifyName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="authorId">
          <el-select v-model="form.authorId" placeholder="请选择作者">
            <el-option
              v-for="item in authorList"
              :key="item.id"
              :label="item.authorStageName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否推荐" prop="bookIsRecommend">
          <el-select v-model="form.bookIsRecommend" placeholder="请选择是否推荐">
            <el-option
              v-for="dict in dict.type.sys_novel_recommend"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书名" />
        </el-form-item>
        <el-form-item label="书籍封面">
          <image-upload v-model="form.bookImage"/>
        </el-form-item>
        <el-form-item label="书籍字数" prop="bookFontNumber">
          <el-input v-model="form.bookFontNumber" placeholder="请输入书籍字数" />
        </el-form-item>
        <el-form-item label="更新说明" prop="bookUpdateText">
          <el-input v-model="form.bookUpdateText" placeholder="请输入更新说明" />
        </el-form-item>
        <el-form-item label="发布时间" prop="bookStartTime">
          <el-date-picker clearable
            v-model="form.bookStartTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="bookUpdateTime">
          <el-date-picker clearable
            v-model="form.bookUpdateTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完结时间" prop="bookEndTime">
          <el-date-picker clearable
            v-model="form.bookEndTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择完结时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="书籍评分" prop="bookScore">
          <el-input v-model="form.bookScore" placeholder="请输入书籍评分" />
        </el-form-item>
        <!--el-form-item label="浏览量" prop="bookBrowseNumber">
          <el-input v-model="form.bookBrowseNumber" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="收藏数量" prop="bookCollectionNumber">
          <el-input v-model="form.bookCollectionNumber" placeholder="请输入收藏数量" />
        </el-form-item>
        <el-form-item label="点赞数量" prop="bookLikeNumber">
          <el-input v-model="form.bookLikeNumber" placeholder="请输入点赞数量" />
        </el-form-item-->
        <el-form-item label="是否付费" prop="bookIsPay">
          <el-select v-model="form.bookIsPay" placeholder="请选择是否付费">
            <el-option
              v-for="dict in dict.type.sys_novel_money"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付费金额" prop="bookPayMoney" v-show="form.bookIsPay == '2'">
          <el-input v-model="form.bookPayMoney" placeholder="请输入付费金额" />
        </el-form-item>
        <!--el-form-item label="付费数量" prop="bookPayNumber">
          <el-input v-model="form.bookPayNumber" placeholder="请输入付费数量" disabled />
        </el-form-item-->
        <el-form-item label="书籍状态" prop="bookStatus">
          <el-radio-group v-model="form.bookStatus">
            <el-radio
              v-for="dict in dict.type.sys_novel_display"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBook, getBook, delBook, addBook, updateBook } from "@/api/novel/book";
import { listAuthor } from "@/api/novel/author";
import { listClassify } from "@/api/novel/classify";

export default {
  name: "Book",
  dicts: ['sys_novel_display', 'sys_novel_money', 'sys_novel_recommend'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 书籍列表表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classifyId: null,
        authorId: null,
        bookIsRecommend: null,
        bookName: null,
        bookIsPay: null,
        bookStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifyId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        authorId: [
          { required: true, message: "作者ID不能为空", trigger: "blur" }
        ],
        bookIsRecommend: [
          { required: true, message: "是否推荐不能为空", trigger: "blur" }
        ],
        bookName: [
          { required: true, message: "书名不能为空", trigger: "blur" }
        ],
        bookScore: [
          { required: true, message: "书籍评分不能为空", trigger: "blur" }
        ],
        bookBrowseNumber: [
          { required: true, message: "浏览量不能为空", trigger: "blur" }
        ],
        bookCollectionNumber: [
          { required: true, message: "收藏数量不能为空", trigger: "blur" }
        ],
        bookLikeNumber: [
          { required: true, message: "点赞数量不能为空", trigger: "blur" }
        ],
        bookIsPay: [
          { required: true, message: "是否付费不能为空", trigger: "change" }
        ],
        bookStatus: [
          { required: true, message: "书籍状态不能为空", trigger: "blur" }
        ],
      },
      //作者列表
      authorList: [],
      //分类列表
      classifyList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询书籍列表列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
        //查询作者列表
        this.getAuthorList();
        //查询分类列表
        this.getClassifyList();
      });
    },
    //查询作者列表
    getAuthorList() {
      let que = {pageSize: 99999};
      listAuthor(que).then(response => {
        this.authorList = response.rows;
      })
    },
    //查询分类列表
    getClassifyList() {
      let que = {pageSize: 99999};
      listClassify(que).then(response => {
        this.classifyList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classifyId: null,
        authorId: null,
        bookIsRecommend: null,
        bookName: null,
        bookImage: null,
        bookFontNumber: null,
        bookUpdateText: null,
        bookStartTime: null,
        bookUpdateTime: null,
        bookEndTime: null,
        bookScore: null,
        bookBrowseNumber: null,
        bookCollectionNumber: null,
        bookLikeNumber: null,
        bookIsPay: null,
        bookPayMoney: null,
        bookPayNumber: null,
        bookStatus: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加书籍列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改书籍列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除书籍列表编号为"' + ids + '"的数据项？').then(function() {
        return delBook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('novel/book/export', {
        ...this.queryParams
      }, `book_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
